package edu.ccut.saturn.studio.saturndata.page;

import java.util.Collections;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import edu.ccut.saturn.component.IValidateInfo;
import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.database.model.DBConnection;
import edu.ccut.saturn.studio.database.model.DBConnectionProvider;
import edu.ccut.saturn.studio.saturndata.dialog.ValidateAddAndEditDialog;
import edu.ccut.saturn.studio.saturndata.model.DataItem;
import edu.ccut.saturn.studio.saturndata.model.DictManager;
import edu.ccut.saturn.studio.saturndata.model.JavaTypeUtils;
import edu.ccut.saturn.studio.saturndata.model.SaturnDataModel;
import edu.ccut.saturn.studio.saturndata.model.ValidateModel;
import edu.ccut.saturn.studio.saturndata.model.ValidateVO;
import edu.ccut.saturn.studio.saturndata.model.DictManager.DictManagerListener;

public class DataItemDetailsPage implements IDetailsPage {
	private Text numberFormatText;
	private Text datetimeFormatText;
	private DataItemPage page;
	private IManagedForm mform;
	private DataItem dataItem;
	private SectionPart secPart;

	private Text columnText;
	private Text columnAliasText;
	private Text typeText;

	private Combo javatypeText;
	private Text lengthText;
	private Text showNameText;
	private Text defaultValueText;

	private Text dictTypeText;
	private Text tablenameText;
	private Text sequenceText;

	private Label imageLabel;

	private Button isbutton;
	private Button addbutton;
	private Button editerbutton;
	private Button removebutton;
	private Table table;
	private TableViewer validatetableViewer;

	private Button isPrimeKeybutton;
	private Button noPrimeKeybutton;
	private Group group2;

	private Shell shell;
	private SaturnDataModel saturnDataModel;

	public DataItemDetailsPage(DataItemPage filePage) {
		this.page = filePage;
		this.saturnDataModel = filePage.getSaturnDataModel();
		this.shell = this.page.getEditor().getSite().getShell();
	}

	public void createContents(Composite composite) {
		TableWrapLayout tablewraplayout = new TableWrapLayout();	
		tablewraplayout.topMargin = 0;
		tablewraplayout.leftMargin = 5;
		tablewraplayout.rightMargin = 0;
		tablewraplayout.bottomMargin = 0;
		composite.setLayout(tablewraplayout);
		FormToolkit formtoolkit = mform.getToolkit();
		createFieldMappingSection(formtoolkit, composite);
	}

	public void selectionChanged(IFormPart part, ISelection selection) {
		IStructuredSelection istructuredselection = (IStructuredSelection) selection;
		if (istructuredselection.size() >= 1) {
			this.dataItem = (DataItem) istructuredselection.getFirstElement();

			this.selChange();
		}
	}

	public void commit(boolean onSave) {
		if (setFormInput(dataItem)) {
			refresh();
		}
	}

	public void dispose() {
	}

	public void initialize(IManagedForm imanagedform) {
		mform = imanagedform;
	}

	public boolean isStale() {
		return false;
	}

	public void refresh() {
		mform.refresh();
	}

	public void setFocus() {
	}

	public boolean setFormInput(Object input) {
		return true;
	}

	public boolean isDirty() {
		return false;
	}

	private void createFieldMappingSection(FormToolkit formtoolkit,
			Composite composite) {
		Section section = createSection(formtoolkit, composite, Messages
				.getString("DetailsPage.fieldproperty")); //$NON-NLS-1$
		Composite compositeall = formtoolkit.createComposite(section);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		compositeall.setLayout(gridLayout);

		final Label ColumnLabel = new Label(compositeall, SWT.NONE);
		ColumnLabel.setText(Messages.getString("DetailsPage.columnname")); //$NON-NLS-1$

		columnText = new Text(compositeall, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_columnText = new GridData(SWT.FILL, SWT.BOTTOM, true,
				false, 2, 1);
		columnText.setLayoutData(gd_columnText);
		columnText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String name = columnText.getText().trim();
				dataItem.setColumn(name);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_COLUMN, name);
			}
		});

		final Label colimnaliasLabel_1 = new Label(compositeall, SWT.NONE);
		colimnaliasLabel_1.setLayoutData(new GridData());
		formtoolkit.adapt(colimnaliasLabel_1, true, true);
		colimnaliasLabel_1.setText(Messages.getString("DetailsPage.anothercolumnname")); //$NON-NLS-1$

		columnAliasText = new Text(compositeall, SWT.BORDER);
		columnAliasText.setTextLimit(50);
		final GridData gd_columnAliasText = new GridData(SWT.FILL, SWT.BOTTOM,
				true, false, 2, 1);
		gd_columnAliasText.verticalIndent = 5;
		columnAliasText.setLayoutData(gd_columnAliasText);
		columnAliasText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String columnAlias = columnAliasText.getText().trim();
				dataItem.setColumnAlias(columnAlias);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_COLUMN_ALIAS, columnAlias);
				if (!SaturnValidation.isValidProgramStr(columnAlias)) {
					MessageDialog.openInformation(null,Messages.getString("DataItemDetailsPage.remend"),Messages.getString("DataItemDetailsPage.anothercolumnnamelimit")); //$NON-NLS-1$ //$NON-NLS-2$
					columnAliasText.setText("");
				}
			}
		});

		final Label TypeLabel = new Label(compositeall, SWT.NONE);
		TypeLabel.setLayoutData(new GridData());
		TypeLabel.setText(Messages.getString("DetailsPage.fieldtype")); //$NON-NLS-1$

		typeText = new Text(compositeall, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_typeText = new GridData(SWT.FILL, SWT.BOTTOM, true,
				false, 2, 1);
		gd_typeText.verticalIndent = 5;
		typeText.setLayoutData(gd_typeText);
		typeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String type = typeText.getText().trim();
				dataItem.setType(type);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_TYPE, type);
			}
		});

		final Label javatypeLabel = new Label(compositeall, SWT.NONE);
		javatypeLabel.setLayoutData(new GridData());
		javatypeLabel.setText(Messages.getString("DetailsPage.javatype")); //$NON-NLS-1$

		javatypeText = new Combo(compositeall, SWT.READ_ONLY);
		javatypeText.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true,
				false, 2, 1));
		javatypeText.setItems(JavaTypeUtils.getSelectedJavaType());
		javatypeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String javaType = javatypeText.getText().trim();
				dataItem.setJavaType(javaType);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_JAVA_TYPE, javaType);
			}
		});

		final Label lengthLabel = new Label(compositeall, SWT.NONE);
		lengthLabel.setLayoutData(new GridData());
		lengthLabel.setText(Messages.getString("DetailsPage.length")); //$NON-NLS-1$
		lengthText = new Text(compositeall, SWT.MULTI | SWT.BORDER);
		lengthText.setTextLimit(5);
		final GridData gd_lengthText = new GridData(SWT.FILL, SWT.BOTTOM, true,
				false, 2, 1);
		gd_lengthText.verticalIndent = 5;
		lengthText.setLayoutData(gd_lengthText);
		lengthText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String length = lengthText.getText().trim();
				dataItem.setLength(length);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_LENGTH, length);
				if (!SaturnValidation.isInt(length)) {
					lengthText.setText("");
				}
				else if(length.length()== 5) {
					MessageDialog.openInformation(null,	(Messages.getString("DataItemDetailsPage.remend")), (Messages.getString("DataItemDetailsPage.maxlength")));       //$NON-NLS-1$ //$NON-NLS-2$
					lengthText.setText(length.substring(0, 4));
				} 
			}
		});

		final Label showNameLabel = new Label(compositeall, SWT.NONE);
		showNameLabel.setLayoutData(new GridData());
		showNameLabel.setText(Messages.getString("DetailsPage.showname")); //$NON-NLS-1$

		showNameText = new Text(compositeall, SWT.BORDER);
		showNameText.setTextLimit(50);
		final GridData gd_showNameText = new GridData(SWT.FILL, SWT.BOTTOM,
				true, false, 2, 1);
		gd_showNameText.verticalIndent = 5;
		showNameText.setLayoutData(gd_showNameText);
		showNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String showName = showNameText.getText().trim();
				dataItem.setShowName(showName);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_SHOW_NAME, showName);
				if (!SaturnValidation.isValidProgramStr(showName ) && !SaturnValidation.isGB2312(showName )) {
					MessageDialog.openInformation(null,	Messages.getString("DataItemDetailsPage.remend"),Messages.getString("DataItemDetailsPage.shownamelimit")); //$NON-NLS-1$ //$NON-NLS-2$
					showNameText.setText("");
				}
			}
		});

		final Label defaultValueLabel = new Label(compositeall, SWT.NONE);
		defaultValueLabel.setLayoutData(new GridData());
		defaultValueLabel.setText(Messages.getString("DetailsPage.defaultvalue")); //$NON-NLS-1$

		defaultValueText = new Text(compositeall, SWT.BORDER);
		final GridData gd_defaultValueText = new GridData(SWT.FILL, SWT.BOTTOM,
				true, false, 2, 1);
		gd_defaultValueText.verticalIndent = 5;
		defaultValueText.setLayoutData(gd_defaultValueText);
		defaultValueText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String defaultValue = defaultValueText.getText().trim();

				dataItem.setDefaultValue(defaultValue);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_DEFAULT_VALUE, defaultValue);
			}
		});

		final Label dictTypeIdLabel = new Label(compositeall, SWT.NONE);
		dictTypeIdLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				true));
		dictTypeIdLabel.setText(Messages.getString("DetailsPage.datadictionary")); //$NON-NLS-1$

		dictTypeText = new Text(compositeall, SWT.BORDER);
		final GridData gd_dictTypeText = new GridData(SWT.FILL, SWT.CENTER,
				true, true);
		gd_dictTypeText.verticalIndent = 5;
		dictTypeText.setLayoutData(gd_dictTypeText);
		dictTypeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String dictTypeId = dictTypeText.getText().trim();
				dataItem.setDictTypeId(dictTypeId);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_DICT_TYPE_ID, dictTypeId);
			}
		});

		imageLabel = new Label(compositeall, SWT.CENTER);
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		final GridData gd_imageLabel = new GridData(SWT.FILL, SWT.BOTTOM,
				false, true);
		imageLabel.setLayoutData(gd_imageLabel);
		imageLabel.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		imageLabel.addMouseListener(new MouseAdapter() {

			public void mouseUp(final MouseEvent e) {
				final ElementListSelectionDialog dialog1 = SaturnDialogUtil
						.createElementListSelectionDialog(shell,
								new SaturnSWTUtils.SaturnLabelDefaultProvider() {

									@Override
									public String getText(Object element) {
										return element.toString();
									}
								});

				dialog1.setTitle(Messages.getString("DetailsPage.searchdatabasefield")); //$NON-NLS-1$

				String connectionName = saturnDataModel.getSaturnData()
						.getConnectionName();

				DictManager.getDictNames(connectionName,
						new DictManagerListener() {
							public void onGetDict(final List<String> dicts) {
								shell.getDisplay().syncExec(new Runnable() {
									public void run() {
										if (dicts != null && !dicts.isEmpty()) {
											dialog1
													.setElements(dicts
															.toArray());

											if (dialog1.open() == Window.OK) {
												String dictName = (String) dialog1
														.getFirstResult();

												dictTypeText.setText(dictName);
											}
										}
									}
								});
							}
						});
			}
		});

		final Label datatimeFormatLabel = new Label(compositeall, SWT.NONE);
		formtoolkit.adapt(datatimeFormatLabel, true, true);
		datatimeFormatLabel.setText(Messages.getString("DetailsPage.dateformat")); //$NON-NLS-1$

		datetimeFormatText = new Text(compositeall, SWT.BORDER);
		formtoolkit.adapt(datetimeFormatText, true, true);
		final GridData gd_datatimeFormatText = new GridData(SWT.FILL,
				SWT.CENTER, true, false, 2, 1);
		gd_datatimeFormatText.verticalIndent = 5;
		datetimeFormatText.setLayoutData(gd_datatimeFormatText);
		datetimeFormatText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String datetimeFormat = datetimeFormatText.getText().trim();
				dataItem.setDatetimeFormat(datetimeFormat);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_DATETIME_FORMAT,
						datetimeFormat);
			}
		});

		final Label numberFormatLabel = new Label(compositeall, SWT.NONE);
		formtoolkit.adapt(numberFormatLabel, true, true);
		numberFormatLabel.setText(Messages.getString("DetailsPage.dataformat")); //$NON-NLS-1$

		numberFormatText = new Text(compositeall, SWT.BORDER);
		formtoolkit.adapt(numberFormatText, true, true);
		final GridData gd_numberFormatText = new GridData(SWT.FILL, SWT.CENTER,
				true, false, 2, 1);
		gd_numberFormatText.verticalIndent = 5;
		numberFormatText.setLayoutData(gd_numberFormatText);
		numberFormatText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String numberFormat = numberFormatText.getText().trim();
				dataItem.setNumberFormat(numberFormat);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_NUMBER_FORMAT, numberFormat);
			}
		});
		final Label tablenameLabel = new Label(compositeall, SWT.NONE);
		tablenameLabel.setLayoutData(new GridData());
		tablenameLabel.setText(Messages.getString("DetailsPage.tablename")); //$NON-NLS-1$

		tablenameText = new Text(compositeall, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_tablenameText = new GridData(SWT.FILL, SWT.BOTTOM,
				true, false, 2, 1);
		gd_tablenameText.verticalIndent = 5;
		tablenameText.setLayoutData(gd_tablenameText);
		tablenameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String tableName = tablenameText.getText().trim();
				dataItem.setTableName(tableName);
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_TABLE_NAME, tableName);
			}
		});

		final Label validatelabel = new Label(compositeall, SWT.NONE);
		validatelabel.setLayoutData(new GridData());
		validatelabel.setText(Messages.getString("DetailsPage.ifvalidate")); //$NON-NLS-1$

		isbutton = new Button(compositeall, SWT.CHECK);
		isbutton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
		isbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				boolean isSelected = isbutton.getSelection();
				dataItem.getValidateUseInfo().setValidate(isSelected);
				saturnDataModel.changeValidateUseAttributeValue(
						dataItem.getId(),
						ValidateModel.VALIDATES_ATTR_IS_VALIDATE, isSelected
								+ ""); //$NON-NLS-1$
			}
		});
		isbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				boolean isSelected = isbutton.getSelection();

				addbutton.setEnabled(isSelected);
				editerbutton.setEnabled(isSelected);
				removebutton.setEnabled(isSelected);
			}
		});
		new Label(compositeall, SWT.NONE);

		validatetableViewer = new TableViewer(compositeall, SWT.MULTI
				| SWT.FULL_SELECTION | SWT.V_SCROLL);
		validatetableViewer
				.setContentProvider(new SaturnSWTUtils.SaturnTableViewerDefaultContentProvider());
		validatetableViewer
				.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {

					@Override
					public String getColumnText(Object obj, int index) {
						IValidateInfo validateInfo = (IValidateInfo) obj;
						if (index == 0) {
							return validateInfo.getName();
						}
						if (index == 1) {
							return validateInfo.getClassTypeStr();
						}

						return super.getColumnText(obj, index);
					}

				});
		table = validatetableViewer.getTable();
		table
				.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 2,
						3));
		table.setHeaderVisible(true); // 显示表头
		table.setLinesVisible(true); // 显示表格线

		final TableColumn newColumnTableColumn_1 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_1.setWidth(123);
		newColumnTableColumn_1.setText(Messages.getString("DetailsPage.name")); //$NON-NLS-1$

		final TableColumn newColumnTableColumn_2 = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_2.setWidth(144);
		newColumnTableColumn_2.setText(Messages.getString("DetailsPage.validateclass")); //$NON-NLS-1$

		addbutton = new Button(compositeall, SWT.NONE);
		addbutton
				.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true));
		addbutton.setText(Messages.getString("DetailsPage.add")); //$NON-NLS-1$
		addbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				openValidateAddDialog();
			}
		});

		editerbutton = new Button(compositeall, SWT.NONE);
		editerbutton.setLayoutData(new GridData());
		editerbutton.setText(Messages.getString("DetailsPage.edit")); //$NON-NLS-1$
		editerbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				openValidateEditerDialog();
			}
		});

		removebutton = new Button(compositeall, SWT.NONE);
		removebutton.setLayoutData(new GridData());
		removebutton.setText(Messages.getString("DetailsPage.delete")); //$NON-NLS-1$
		removebutton.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings("unchecked")//$NON-NLS-1$
			public void widgetSelected(SelectionEvent event) {
				IStructuredSelection selection = (IStructuredSelection) validatetableViewer
						.getSelection();
				ValidateVO validateVO = (ValidateVO) selection
						.getFirstElement();
				if (selection.isEmpty()) {
					SaturnDialogUtil.openInformation(null, Messages
							.getString("DetailsPage.chooseitemfordelete")); //$NON-NLS-1$ //$NON-NLS-2$
				} else {
					if (SaturnDialogUtil
							.openConfirm(
									null,
									Messages.getString("DetailsPage.remend"), Messages.getString("DetailsPage.confirmdeleteitem"))) { //$NON-NLS-1$ //$NON-NLS-2$
						List<IValidateInfo> validatelist = (List<IValidateInfo>) validatetableViewer
								.getInput();
						validatelist.remove(validateVO);// 删除容器中的数据
						dataItem.getValidateUseInfo().setValidateInfos(
								validatelist);
						validatetableViewer.remove(validateVO);// 删除表格中的数据

						saturnDataModel.changeValidates(dataItem.getId(),
								validatelist);
					}
				}
			}
		});

		final Label isPrimeKeyLabel = new Label(compositeall, SWT.NONE);
		isPrimeKeyLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				false, false));
		isPrimeKeyLabel.setText(Messages.getString("DetailsPage.ifprimarykey")); //$NON-NLS-1$

		isPrimeKeybutton = new Button(compositeall, SWT.RADIO);
		isPrimeKeybutton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
				false));
		isPrimeKeybutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				dataItem.setIsPrimaryKey("true"); //$NON-NLS-1$
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_IS_PRIMARY_KEY, "true"); //$NON-NLS-1$
				group2.setVisible(true);

			}
		});
		isPrimeKeybutton.setText(Messages.getString("DetailsPage.yes")); //$NON-NLS-1$
		isPrimeKeybutton.setSelection(false);

		noPrimeKeybutton = new Button(compositeall, SWT.RADIO);
		noPrimeKeybutton.setLayoutData(new GridData());
		noPrimeKeybutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				dataItem.setIsPrimaryKey("false"); //$NON-NLS-1$
				saturnDataModel.changeFiledAttributeValue(dataItem.getId(),
						SaturnDataModel.FIELD_ATTR_IS_PRIMARY_KEY, "false"); //$NON-NLS-1$
				group2.setVisible(false);
			}
		});
		noPrimeKeybutton.setText(Messages.getString("DetailsPage.no")); //$NON-NLS-1$
		noPrimeKeybutton.setSelection(true);

		group2 = new Group(compositeall, SWT.NONE);
		group2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,
				3, 1));
		group2.setVisible(false);
		group2.setText(Messages.getString("DetailsPage.sequencestyle")); //$NON-NLS-1$
		final GridLayout gridLayout2 = new GridLayout(3, false);
		group2.setLayout(gridLayout2);

		final Label classTypeLabel1 = new Label(group2, SWT.NONE);
		classTypeLabel1.setAlignment(SWT.CENTER);
		GridData gridDataclassTypeLabel = new GridData(SWT.CENTER, SWT.CENTER,
				false, false);
		gridDataclassTypeLabel.heightHint = 15;
		gridDataclassTypeLabel.widthHint = 74;
		classTypeLabel1.setLayoutData(gridDataclassTypeLabel);
		classTypeLabel1.setText(Messages.getString("DetailsPage.sequencename")); //$NON-NLS-1$

		sequenceText = new Text(group2, SWT.SINGLE | SWT.NULL);
		GridData gridDatajsmethodsequence = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gridDatajsmethodsequence.widthHint = 232;
		sequenceText.setLayoutData(gridDatajsmethodsequence);

		final Button sequencetableBrowerButton = new Button(group2, SWT.NONE);
		sequencetableBrowerButton.setText(Messages.getString("DetailsPage.browse")); //$NON-NLS-1$
		sequencetableBrowerButton.setLayoutData(new GridData(50, SWT.DEFAULT));
		sequencetableBrowerButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				ElementListSelectionDialog dialog = SaturnDialogUtil
						.createElementListSelectionDialog(shell,
								new SaturnSWTUtils.SaturnLabelDefaultProvider() {
									@Override
									public Image getImage(Object element) {
										return Activator.getImage(Images.SATURNDATA_SEQUENCE_TABLE); 
									}

									@Override
									public String getText(Object element) {
										return element.toString();
									}
								});

				dialog.setTitle(Messages.getString("DetailsPage.choosesequencename")); //$NON-NLS-1$

				String name = saturnDataModel.getSaturnData().getConnectionName();
				DBConnection dbConnection = DBConnectionProvider.getInstance()
						.getConnection(name);

				try {
					List<String> sequenceList = dbConnection.getSequenceNames();
					Collections.sort(sequenceList);

					dialog.setElements(sequenceList.toArray());
					if (dialog.open() == Window.OK) {
						sequenceText.setText((String) dialog.getFirstResult());

						String sequence = sequenceText.getText().trim();
						dataItem.setSequenceName(sequence);
						saturnDataModel.changeFiledAttributeValue(dataItem
								.getId(),
								SaturnDataModel.FIELD_ATTR_SEQUENCE_NAME,
								sequence);
					}
				} catch (Exception e1) {
					SaturnDialogUtil.openError(shell, Messages
							.getString("DetailsPage.cannotgetsequence")); //$NON-NLS-1$
					e1.printStackTrace();
				} finally {
					if (dbConnection != null) {
						try {
							dbConnection.closeConnection();
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});

		formtoolkit.paintBordersFor(compositeall);
		section.setClient(compositeall);
	}

	private Section createSection(FormToolkit formtoolkit, Composite composite,
			String s) {
		Section section = formtoolkit.createSection(composite, 384);
		section.setActiveToggleColor(formtoolkit.getHyperlinkGroup()
				.getActiveForeground());
		section.setToggleColor(formtoolkit.getColors().getColor(
				"org.eclipse.ui.forms.SEPARATOR")); //$NON-NLS-1$
		section.marginHeight = 5;
		section.marginWidth = 5;
		section.setText(s);
		secPart = new SectionPart(section, formtoolkit, SWT.NONE);
		secPart.initialize(mform);
		mform.addPart(secPart);
		TableWrapData tablewrapdata = new TableWrapData(128, 16);
		tablewrapdata.grabHorizontal = true;
		section.setLayoutData(tablewrapdata);
		return section;
	}

	/**
	 * 添加表格方法
	 * 
	 * @param parent
	 * 
	 */
	private void openValidateAddDialog() {
		List<IValidateInfo> validatelist = this.dataItem.getValidateUseInfo()
				.getValidateInfos();

		ValidateAddAndEditDialog addDialog = new ValidateAddAndEditDialog(shell);
		if (addDialog.open() == IDialogConstants.OK_ID) {
			IValidateInfo validateInfo = addDialog.getValidateVO();

			if (validateInfo != null) {
				validatelist.add(validateInfo);

				validatetableViewer.setInput(validatelist);
				saturnDataModel.changeValidates(dataItem.getId(), validatelist);
			}
		}
	}

	/**
	 * 编辑表格方法
	 * 
	 * @param parent
	 * 
	 */
	private void openValidateEditerDialog() {
		List<IValidateInfo> validatelist = this.dataItem.getValidateUseInfo()
				.getValidateInfos();

		ValidateVO validateVO = null;
		

		IStructuredSelection selection = (IStructuredSelection) validatetableViewer
				.getSelection();

		if (!selection.isEmpty()) {
			validateVO = (ValidateVO) validatelist.get(validatetableViewer
					.getTable().getSelectionIndex());
		} else {
			if (validatelist != null && !validatelist.isEmpty()) {
				validateVO = (ValidateVO) validatelist.get(0);
			} else {
				SaturnDialogUtil.openInformation(null, Messages
						.getString("DetailsPage.validatecannotexist")); //$NON-NLS-1$
			}
		}

		ValidateAddAndEditDialog addDialog = new ValidateAddAndEditDialog(shell);
		addDialog.setValidateVO(validateVO);
		if (addDialog.open() == IDialogConstants.OK_ID) {
			ValidateVO editValidateVO = addDialog.getValidateVO();

			if (editValidateVO != null) {
				validateVO.setKey(editValidateVO.getKey());
				validateVO.setName(editValidateVO.getName());
				validateVO.setClassTypeStr(editValidateVO.getClassTypeStr());
				validateVO.setJSMethodName(editValidateVO.getJSMethodName());
				validateVO.setCustemJSMethodInfo(editValidateVO
						.getCustemJSMethodInfo());
				validateVO.setDescription(editValidateVO.getDescription());
				validateVO.setValidateType(editValidateVO.getValidateType());
				validateVO.setErrorInfo(editValidateVO.getErrorInfos());
				validateVO.setEventInfos(editValidateVO.getEventInfos());
				validateVO.setArgs(editValidateVO.getArgs());

				validatetableViewer.setInput(validatelist);
				saturnDataModel.changeValidates(dataItem.getId(), validatelist);
			}
		}
	}

	public void clear() {
		columnText.setText(""); //$NON-NLS-1$
		columnAliasText.setText("");//$NON-NLS-1$
		typeText.setText("");// 数据库中的类型; 
		javatypeText.setText(""); //$NON-NLS-1$
		lengthText.setText(""); 
		showNameText.setText(""); //$NON-NLS-1$
		defaultValueText.setText(""); 

		dictTypeText.setText(""); //$NON-NLS-1$
		datetimeFormatText.setText("");//$NON-NLS-1$
		numberFormatText.setText("");//$NON-NLS-1$
		tablenameText.setText(""); //$NON-NLS-1$

		isbutton.setSelection(false);
		addbutton.setEnabled(false);
		editerbutton.setEnabled(false);
		removebutton.setEnabled(false);
		noPrimeKeybutton.setSelection(false);
		group2.setVisible(false);
	}

	private void selChange() {
		columnText.setText(dataItem.getColumn());
		columnAliasText.setText(dataItem.getColumnAlias());// 别名;
		typeText.setText(dataItem.getType());// 数据库中的类型;

		javatypeText.setText(dataItem.getJavaType());

		lengthText.setText(dataItem.getLength());
		showNameText.setText(dataItem.getShowName());
		defaultValueText.setText(dataItem.getDefaultValue());
		dictTypeText.setText(dataItem.getDictTypeId());
		datetimeFormatText.setText(dataItem.getDatetimeFormat());
		numberFormatText.setText(dataItem.getNumberFormat());
		tablenameText.setText(dataItem.getTableName());

		isbutton.setSelection(dataItem.getValidateUseInfo().isValidate());
		addbutton.setEnabled(isbutton.getSelection());
		editerbutton.setEnabled(isbutton.getSelection());
		removebutton.setEnabled(isbutton.getSelection());

		validatetableViewer.setInput(dataItem.getValidateUseInfo()
				.getValidateInfos());

		boolean isPrimaryKey = dataItem.getIsPrimaryKey().equals("true"); //$NON-NLS-1$
		if (isPrimaryKey) {
			sequenceText.setText(dataItem.getSequenceName());
		}
		isPrimeKeybutton.setSelection(isPrimaryKey);
		noPrimeKeybutton.setSelection(!isPrimaryKey);
		group2.setVisible(isPrimaryKey);
	}
}
